# -*- coding: utf-8 -*-

import sip
sip.setapi('QString', 2)
sip.setapi('QStringList', 2)
sip.setapi('QVariant', 2)

from PyQt4 import QtCore

class PlayerConfigController(QtCore.QObject):
    """FIXME: troppo complicato il passaggio di azioni dalla view al controller 
    al demone"""
    def __init__(self, server_ctrl, view):
        QtCore.QObject.__init__(self)
        self.server_ctrl = server_ctrl
        self.view = view
        #Signal
        self.server_ctrl.client_connected.connect(self.setup)
        self.server_ctrl.options_changed.connect(self.dispatcher)
        self.view.random_button.clicked.connect(self.set_random)
        self.view.repeat_combobox.currentIndexChanged.connect(self.set_repeat)

    def setup(self, connected):
        if connected:
            self.view.random_button.setEnabled(True)
            self.view.repeat_combobox.setEnabled(True)            
            if self.server_ctrl.model.info_status["random"]:
                self.view.random_button.setChecked(True)
            if self.server_ctrl.model.info_status["repeat"]:
                if self.server_ctrl.model.info_status["single"]:
                    self.view.repeat_combobox.setCurrentIndex(1)
                else:
                    self.view.repeat_combobox.setCurrentIndex(2)

    def set_random(self, checked):
        if checked:
            if self.server_ctrl.model.info_status["random"]:
                return
            self.server_ctrl.client.random(1)
        else:
            if not self.server_ctrl.model.info_status["random"]:
                return
            self.server_ctrl.client.random(0)
        self.server_ctrl.model.update_statusInfo(self.server_ctrl.client.status())
    
    def set_repeat(self, index):
        if index == 0:
            if not self.server_ctrl.model.info_status["repeat"]:
                return
            self.server_ctrl.client.single(0)
            self.server_ctrl.client.repeat(0)
        elif index == 1:
            if (self.server_ctrl.model.info_status["repeat"] and 
                self.server_ctrl.model.info_status["single"]):
                return
            self.server_ctrl.client.single(1)
            self.server_ctrl.client.repeat(1)
        elif index == 2:
            if (self.server_ctrl.model.info_status["repeat"] and 
                not self.server_ctrl.model.info_status["single"]):
                return
            self.server_ctrl.client.single(0)
            self.server_ctrl.client.repeat(1)
        self.server_ctrl.model.update_statusInfo(self.server_ctrl.client.status())
    
    def dispatcher(self, msg):
        if msg == "random":
            if self.view.random_button.isChecked():
                self.view.random_button.setChecked(False)
            else:
                self.view.random_button.setChecked(True)
        elif msg == "repeat_pl":
            if self.view.repeat_combobox.currentIndex() == 0:
                self.view.repeat_combobox.setCurrentIndex(2)
            elif self.view.repeat_combobox.currentIndex() == 1:
                self.view.repeat_combobox.setCurrentIndex(2)
        elif msg == "repeat_song":
            if self.view.repeat_combobox.currentIndex() == 0:
                self.view.repeat_combobox.setCurrentIndex(1)
            elif self.view.repeat_combobox.currentIndex() == 2:
                self.view.repeat_combobox.setCurrentIndex(1)         
        elif msg == "repeat_norm":
            if self.view.repeat_combobox.currentIndex() == 1:
                self.view.repeat_combobox.setCurrentIndex(0)
            elif self.view.repeat_combobox.currentIndex() == 2:
                self.view.repeat_combobox.setCurrentIndex(0)
        elif msg == "stop_after":
            if self.view.repeat_combobox.currentIndex() == 1:
                self.view.repeat_combobox.setCurrentIndex(0)
            elif self.view.repeat_combobox.currentIndex() == 2:
                self.view.repeat_combobox.setCurrentIndex(0)
